package com.ly.web.controller.erp;

import java.util.List;

import com.ly.common.constant.HttpStatus;
import com.ly.heart.domain.dto.SupplierQueryDto;
import com.ly.heart.service.ISupplierService;
import com.ly.heart.service.king.base.SupplierPushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.heart.domain.SupplierEntity;
import com.ly.common.utils.poi.ExcelUtil;
import com.ly.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 供应商Controller
 *
 * @author cxl
 * @date 2024-06-16
 */
@Api ( value = "供应商", tags = "供应商" )
@RestController
@RequestMapping ( "/heart/Supplier" )
public class SupplierController extends BaseController {

    @Resource
    private ISupplierService supplierService;
    @Resource
    private SupplierPushService supplierPushService;

    /**
     * 查询供应商列表
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Supplier:list')" )
    @GetMapping ( "/list" )
    @ApiOperation ( "查询供应商列表" )
    public TableDataInfo list ( SupplierEntity supplierEntity ) {
        List<SupplierEntity> list = supplierService.selectSupplierEntityList ( supplierEntity );
        Integer integer = supplierService.queryCount ( supplierEntity );
        TableDataInfo tableDataInfo = new TableDataInfo ();
        tableDataInfo.setRows ( list );
        tableDataInfo.setTotal ( integer );
        tableDataInfo.setCode ( HttpStatus.SUCCESS );
        return tableDataInfo;
    }

    /**
     * 导出供应商列表
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Supplier:export')" )
    @Log ( title = "供应商", businessType = BusinessType.EXPORT )
    @GetMapping ( "/export" )
    @ApiOperation ( "导出供应商列表" )
    public AjaxResult export ( SupplierEntity supplierEntity ) {
        List<SupplierEntity> list = supplierService.selectSupplierEntityList ( supplierEntity );
        ExcelUtil<SupplierEntity> util = new ExcelUtil<SupplierEntity> ( SupplierEntity.class );
        return util.exportExcel ( list, "Supplier" );
    }

    /**
     * 获取供应商详细信息
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Supplier:query')" )
    @GetMapping ( value = "/{id}" )
    @ApiOperation ( "获取供应商详细信息" )
    public AjaxResult getInfo ( @PathVariable ( "id" ) String id ) {
        return AjaxResult.success ( supplierService.selectSupplierEntityById ( id ) );
    }

    /**
     * 新增供应商
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Supplier:add')" )
    @Log ( title = "暂存供应商", businessType = BusinessType.INSERT )
    @PostMapping ( "draft" )
    @ApiOperation ( "暂存新增供应商" )
    public AjaxResult draft ( @RequestBody SupplierEntity supplierEntity ) {
        return supplierService.draft ( supplierEntity );
    }

    /**
     * 新增供应商
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Supplier:add')" )
    @Log ( title = "提交供应商", businessType = BusinessType.INSERT )
    @PostMapping ( "submit" )
    @ApiOperation ( "提交新增供应商" )
    public AjaxResult submit ( @RequestBody SupplierEntity supplierEntity ) {
        return supplierService.submit ( supplierEntity );
    }

    /**
     * 修改供应商
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Supplier:edit')" )
    @Log ( title = "供应商", businessType = BusinessType.UPDATE )
    @PutMapping
    @ApiOperation ( "修改供应商" )
    public AjaxResult edit ( @RequestBody SupplierEntity supplierEntity ) {
        return toAjax ( supplierService.updateSupplierEntity ( supplierEntity ) );
    }

    /**
     * 删除供应商
     */
    @PreAuthorize ( "@ss.hasPermi('heart:Supplier:remove')" )
    @Log ( title = "供应商", businessType = BusinessType.DELETE )
    @DeleteMapping ( "/{ids}" )
    @ApiOperation ( "删除供应商" )
    public AjaxResult remove ( @PathVariable String[] ids ) {
        return toAjax ( supplierService.deleteSupplierEntityByIds ( ids ) );
    }

    @Log ( title = "禁用供应商", businessType = BusinessType.DELETE )
    @RequestMapping ( "/disabled" )
    @ApiOperation ( "禁用币别" )
    public AjaxResult disabled ( String id ) {
        return supplierService.disable ( id );
    }

    @PostMapping ( "/dialog" )
    @ApiOperation ( "供应商选择dialog" )
    public AjaxResult getSupplier(@RequestBody SupplierQueryDto queryDto){
        return supplierService.getSupplier(queryDto);
    }

    @GetMapping ("/push/{supplierId}")
    @ApiOperation ("供应商推送")
    public AjaxResult push(@PathVariable String supplierId){
        supplierPushService.push(supplierId);
        return AjaxResult.success("推送成功！");
    }


}
